package dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.criteria.CriteriaQuery;
import javax.transaction.Transaction;

import util.JPAUtil;
import model.Servico;

public class ServicoDAO {

	public void insert(Servico servico) {

		EntityManager em = JPAUtil.getEntityManager();
		EntityTransaction tx = em.getTransaction();
		tx.begin();
		em.persist(servico);
		tx.commit();
		em.clear();
	}
	
	public void update(Servico servico){
		
		EntityManager em = JPAUtil.getEntityManager();
		EntityTransaction tx = em.getTransaction();
		tx.begin();
		em.merge(servico);
		tx.commit();
		em.clear();
	}
	
	public void delete(Servico servico){
		
		EntityManager em = JPAUtil.getEntityManager();
		EntityTransaction tx = em.getTransaction();
		tx.begin();
		em.remove(em.merge(servico));
		tx.commit();
		em.clear();
	}
	
	public List<Servico> selectAll() {
		
		EntityManager em = JPAUtil.getEntityManager();
		CriteriaQuery<Servico> query = em.getCriteriaBuilder().createQuery(Servico.class);
		query.select(query.from(Servico.class));

		List<Servico> lista = em.createQuery(query).getResultList();

		em.close();
		return lista;
	}
}
